import pygame,random
import mod_game,mod_item,mod_direction,mod_line
import mod_vector,mod_actor
import mod_entity
import mod_ghost
from items.grenade import grenadePrototype
from items.medpack import medpackPrototype
from items.vibrosword import vibroswordPrototype
from items.armor import fireproofSuitPrototype
from characters.janitor import janitorPrototype
from characters.marine import marinePrototype
from characters.slug import slugPrototype
from characters.turret import turretPrototype
from features.barrel import barrelPrototype
from features.door import doorPrototype
from features.wall import wallPrototype

class Level:
    def __init__(self,size,game,number):
        self.spaceMap = {}
        self.entityMap = {}
        self.itemMap = {}
        self.actors = ['ENDTURN']
        self.size = size
        self.width,self.height = size
        self.number = number
        self.game = game
    
    # For now, find a random location for the player and put them there.
    def addPlayer(self,player):
        validCoords = False
        while not validCoords:
            coords = random.choice(self.spaceMap.keys())
            if coords not in self.entityMap:
                validCoords = True
        player.setLocation((self,coords))
    
    def addActor(self,actor,beginningOfTurn=True):
        sentinelIndex = self.actors.index('ENDTURN')
        if beginningOfTurn:
            # Go after the sentinel
            if sentinelIndex == (len(self.actors)-1):
                self.actors.insert(0,actor)
            else:
                self.actors.insert(sentinelIndex+1,actor)
        else:
            # Go before the sentinel
            self.actors.insert(sentinelIndex,actor)
    
    def removeActor(self,actor):
        self.actors.remove(actor)
    
    def processActors(self,game):
        finished = False
        while not finished:
            actor = self.actors.pop(0)
            self.actors.append(actor)
            if actor == 'ENDTURN':
                finished = True
            else:
                actor.takeTurn()
            
    def propagateGhost(self,ghost,otherLocation=None):
        # Only propagate the ghost if it's visible.
        if isinstance(ghost,mod_ghost.EntityGhost) or ghost.visible:
            memories = set()
            location = ghost.location
            if location and not isinstance(location,mod_entity.Entity):
                level,coords = location
            else:
                level,coords = None,None
            
            if otherLocation and not isinstance(otherLocation,mod_entity.Entity):
                otherLevel,otherCoords = otherLocation
            else:
                otherLevel,otherCoords = None,None
            # We should probably not have to add up the brains every time:
            for actor in self.actors:
                if actor != 'ENDTURN':
                    memories.add(actor.brain.memory)
            for memory in memories:
                if (coords and memory.canSeeLocation(location)) or (otherCoords and memory.canSeeLocation(otherLocation)):
                    memory.updateGhost(ghost,self)

def chooseEnemy():
    deck = []
    deck.append(marinePrototype)
    deck.append(marinePrototype)
    deck.append(turretPrototype)
    deck.append(turretPrototype)
#    deck.append(barrelPrototype)
#    deck.append(barrelPrototype)
    deck.append(janitorPrototype)
    deck.append(slugPrototype)
    prototype = random.choice(deck)
    return prototype.instantiate()

def chooseItem():
    deck = []
    deck.append(vibroswordPrototype)
    deck.append(medpackPrototype)
    deck.append(medpackPrototype)
    deck.append(grenadePrototype)
    deck.append(fireproofSuitPrototype)
    prototype = random.choice(deck)
    return prototype.instantiate()

def setUpEmptyLevel(level):
    for x in range(level.width):
        for y in range(level.height):
            level.spaceMap[(x,y)] = '.'
            
def drawEntityLine(level,point1,point2,prototype):
    line = mod_line.generateLine(point1,point2,None,False)
    for space in line:
        entity = prototype.instantiate()
        placeEntity(entity,(level,space))
    return line
    
def printLevel(level):
    for y in range(0,level.height):
        line = ''
        for x in range(0,level.width):
            entity = level.entityMap.get((x,y))
            if entity and entity.prototype == wallPrototype:
                line += '#'
            elif entity and entity.prototype == doorPrototype:
                line += '+'
            elif entity and entity.prototype == barrelPrototype:
                line += 'O'
            else:
                line += level.spaceMap[(x,y)]
        print line
